﻿#include "LPSwitchStyleWidget.h"

#include <QLabel>
#include <QVariant>
#include <QPushButton>
#include <QGridLayout>
#include <QVBoxLayout>

struct LPSwitchStyleWidgetPrivate
{
    LPSwitchStyleWidgetPrivate()
    {

    }
    QGridLayout* _GridLayout;
    typedef QList< QPushButton* > PushButtonArray;
    PushButtonArray _PushButtonArray;
};

const int g_MaxRow = 3;
LPSwitchStyleWidget::LPSwitchStyleWidget(QWidget * parent):
    IWidget(parent),_P(new LPSwitchStyleWidgetPrivate)
{
    InitControlWidget();
    SetWindowTitle(u8"风格切换");
    SetThemeStyle(ThemeStyle::Blue);
}

LPSwitchStyleWidget::~LPSwitchStyleWidget()
{

}

void LPSwitchStyleWidget::AddStyleItem(const QString &Name, const ButtonStyle Theme)
{
    QPushButton * pushButton = new QPushButton( Name );
    pushButton->setProperty("Style",Theme);
    pushButton->setMinimumSize(150,150);
    int Colnum = _P->_PushButtonArray.size()%g_MaxRow;
    int row = _P->_PushButtonArray.size()/g_MaxRow;
    _P->_GridLayout->addWidget( pushButton, row, Colnum );
    _P->_PushButtonArray.append( pushButton );
    connect(pushButton,&QPushButton::clicked,this,&LPSwitchStyleWidget::On_Button_clicked);
}

void LPSwitchStyleWidget::InitControlWidget()
{
    _P->_GridLayout = new QGridLayout(this);
    _P->_GridLayout->setMargin(5);
    _P->_GridLayout->setContentsMargins(0,0,0,0);

    SetContentWidget(_P->_GridLayout);

    AddStyleItem(u8"暗黑色调",BlackStyle);
    AddStyleItem(u8"深夜蓝色",BlueStyle);
    AddStyleItem(u8"甜蜜橙色",OrangeStyle);
    AddStyleItem(u8"绿色色调",GreenStyle);
}

void LPSwitchStyleWidget::On_Button_clicked()
{
    QPushButton* button = (QPushButton*)sender();
    if(button)
    {
        QString btnStyle;
        ButtonStyle style = (ButtonStyle)button->property("Style").toInt();
        if( style == BlackStyle )
        {
            btnStyle = QString("QPushButton\
            {\
                color:#FAFAFA;\
                font: 22px \"Microsoft YaHei\";\
                border:3px solid #757575;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #757575,stop:0.8 #E0E0E0);\
                border-radius:7px;\
            }\
            QPushButton:hover\
            {\
                color:#F5F5F5;\
                border:3px solid #E0E0E0;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #E0E0E0,stop:0.8 #9E9E9E);\
                border-radius:7px;\
            }\
            QPushButton:pressed\
            {\
                color:#EEEEEE;\
                border:3px solid #8D6E63;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #8D6E63,stop:0.8 #424242);\
                border-radius:7px;\
            }");
        }
        else if( style == BlueStyle )
        {
            btnStyle = QString("QPushButton\
            {\
                color:#64FFDA;\
                font: 22px \"Microsoft YaHei\";\
                border:3px solid #03A9F4;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #03A9F4,stop:0.8 #E1F5FE);\
                border-radius:7px;\
            }\
            QPushButton:hover\
            {\
                color:#1DE9B6;\
                border:3px solid #B3E5FC;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #B3E5FC,stop:0.8 #03A9F4);\
                border-radius:7px;\
            }\
            QPushButton:pressed\
            {\
                color:#00BFA5;\
                border:3px solid #01579B;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #01579B,stop:0.8 #03A9F4);\
                border-radius:7px;\
            }");
        }
        else if( style == OrangeStyle )
        {
            btnStyle = QString("QPushButton\
            {\
                color:#212121;\
                font: 22px \"Microsoft YaHei\";\
                border:5px solid #FFA000;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #FFA000,stop:0.8 #FBC02D);\
                border-radius:7px;\
            }\
            QPushButton:hover\
            {\
                color:#EEEEEE;\
                border:5px solid #FFC107;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #FFC107,stop:0.8 #FFF59D);\
                border-radius:7px;\
            }\
            QPushButton:pressed\
            {\
                color:#5D4037;\
                border:5px solid #FF6D00;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #FF6D00,stop:0.8 #E65100);\
                border-radius:7px;\
            }");
        }
        else if( style == GreenStyle )
        {
            btnStyle = QString("QPushButton\
            {\
                color:#212121;\
                font: 22px \"Microsoft YaHei\";\
                border:5px solid #B9F6CA;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #B9F6CA,stop:0.8 #76FF03);\
                border-radius:7px;\
            }\
            QPushButton:hover\
            {\
                color:#EEEEEE;\
                border:5px solid #B9F6CA;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #B9F6CA,stop:0.8 #CCFF90);\
                border-radius:7px;\
            }\
            QPushButton:pressed\
            {\
                color:#5D4037;\
                border:5px solid #69F0AE;\
                background:qradialgradient(spread:pad,cx:0.5,cy:0.5,radius:0.5,fx:0.5,fy:0.5,stop:0.6 #69F0AE,stop:0.8 #B2FF59);\
                border-radius:7px;\
            }");
        }
        for (int i=0;i<_P->_PushButtonArray.size();i++) {
            _P->_PushButtonArray.at(i)->setStyleSheet(btnStyle);
        }
    }
}

